package com.base.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.ModelAndView;

import com.base.util.edit.MyEditor;

public class BaseAction {
	public static final String SUCCESS = "success";
	public static final String MSG = "msg";
	public static final String DATA = "data";
	public static final String LOGOUT_FLAG = "logoutFlag";

	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Date.class,
				new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
		binder.registerCustomEditor(Integer.TYPE, new MyEditor());
	}

	public String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	public String getReqUrl(HttpServletRequest request) {
		String port = Integer.toString(request.getServerPort());
		if ("80".equals(port)) {
			port = "";
		} else {
			port = ":" + port;
		}
		return

		request.getScheme() + "://" + request.getServerName() + port + request.getContextPath() + "/";
	}

	public Map<String, Object> getRootMap() {
		Map<String, Object> rootMap = new HashMap<String, Object>();

		return rootMap;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView forword(String viewName, Map context) {
		return new ModelAndView(viewName, context);
	}

	public ModelAndView error(String errMsg) {
		return new ModelAndView("error");
	}

	public void sendSuccessMessage(HttpServletResponse response, String message) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("success", Boolean.valueOf(true));
		result.put("msg", message);
		HtmlUtil.writerJson(response, result);
	}

	public void sendFailureMessage(HttpServletResponse response, String message) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("success", Boolean.valueOf(false));
		result.put("msg", message);
		HtmlUtil.writerJson(response, result);
	}

	public void printString(String str, HttpServletResponse response) {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");

		System.out.println("-------------response--------------------");
		System.out.println(str);
		System.out.println("-------------response--------------------");
		try {
			PrintWriter out = response.getWriter();
			out.print(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getJson(HttpServletRequest request) {
		InputStream input = null;
		BufferedReader reader = null;
		String temp = null;
		StringBuilder sb = new StringBuilder();
		try {
			input = request.getInputStream();
			reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
			while ((temp = reader.readLine()) != null) {
				sb.append(temp);
			}
		} catch (Exception e) {
			e.printStackTrace();
			try {
				input.close();
				reader.close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				input.close();
				reader.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		System.out.println("----------------request---------------");
		System.out.println(sb.toString());
		System.out.println("----------------request---------------");
		return sb.toString();
	}
}
